package com.biovr.backend.service;

import com.biovr.backend.domain.Expirement;
import com.biovr.backend.repository.ExpirementRepository;
import com.biovr.backend.utils.DateTimeUtils;
import com.biovr.backend.utils.QrCodeUtils;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by monodev on 2017/5/8.
 */
@Service
public class ExpirementServiceImpl implements ExpirementService {
    @Autowired
    private ExpirementRepository expirementRepository;
    @Autowired
    private QrCodeUtils qrCodeUtils;
    @Autowired
    private DateTimeUtils dateTimeUtils;
//    @Value("${web.upload-path}")
//    private String uploadPath;
    @Value("${tbname.expirement}")
    private String tbName;
    /**
     * 增加实验
     * @param request
     * @param expm 实验 ，必填
     * @return
     */
    public Expirement addExpirement(HttpServletRequest request, Expirement expm) {
        expm.setTableName(tbName);
        expm.setStatus(true);
        expm.setCreateTime(dateTimeUtils.getCurrDateTimeString());
        expm = expirementRepository.save(expm);
        String qrcode = generateQrcode(request,expm);
        expm.setQCode(qrcode);
        expm= expirementRepository.save(expm);
        return expm;
    }

    /**
     * 重新生成二维码
     * @param request
     * @param expmid
     * @return
     */
    public String regenerateQrcode(HttpServletRequest request, String expmid) throws Exception{
        Expirement expm = expirementRepository.findOne(expmid);
        if(null!=expm){
            qrCodeUtils.deleteQrcode(expm.getQCode());
            String qrcode = generateQrcode(request,expm);
            expm.setQCode(qrcode);
            expirementRepository.save(expm);
            return expm.getQCode();
        }
        return null;
    }
    /**
     * 生成二维码
     * @param request
     * @param expm
     * @return
     */
    private String generateQrcode(HttpServletRequest request, Expirement expm){
        //生成二维码
        //获得根请求地址
        //int port= -1;
       // port=request.getLocalPort();
        //String basePath = request.getScheme()+"://"+request.getServerName()+(port==-1?"/":":"+port+"/");
        String basePath = request.getScheme()+"://"+request.getServerName()+"/";
        //根据rest规则生成链接
        String qCodeContent = basePath+"expirements/"+expm.getId();
        try {
            //这是base64编码二维码，因生成的二维码数据量太大，已弃用
            // newExpm.setQCode(qrCodeUtils.getBase64QRCode(qCodeContent));
            String qCodeFileName = UUID.randomUUID().toString().replace("-","")+".jpg";
            qrCodeUtils.saveQRCodeToFile(qCodeContent,qCodeFileName,null);
            //expm.setQCode(qCodeFileName);
           return qCodeFileName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据ID删除实验
     * @param id
     */
    public  boolean deleteExpirement(String id) throws Exception{
        if (expirementRepository.exists(id))
        {
            Expirement e = expirementRepository.findOne(id);
            qrCodeUtils.deleteQrcode(e.getQCode());
            expirementRepository.delete(e);
            return true;
        }
        return  false;
    }



//    private void deleteResources(Expirement expm){
//        //删除所有实验资源
//         List<UpFile> res = upFileRepository.getByTableNameAndTableId("${tablename.expirement}",expm.getId());
//          resourceFileUtils.deleteResourceFile(res);
//          expirementRepository.delete(expm);
//    }
}
